#include "Popup/Popup_giftParcel_3.h"
#include "Util/ImageManager.h"
#include "Util/SoundManager.h"
USING_NS_CC;

bool Popup_giftParcel_3::init()
{
	if (!CCLayerColor::initWithColor(ccc4(0,0,0,0)))
	{
		return false;
	}

	winSize = CCDirector::sharedDirector()->getWinSize();

	mainLayer = CCLayer::create();
	mainLayer->setPosition(ccp(-winSize.width,0));
	this->addChild(mainLayer);

	bg_block = CCSprite::create("charge_gift_bg_3.png");
	bg_block->setAnchorPoint(ccp(0,0));
	mainLayer->addChild(bg_block);

	btn_ok = ImageManager::getSpriteWithXml2(kImageSheet_freemium,"button_ok");
	btn_ok->setAnchorPoint(ccp(0.5f,0.0f));
	btn_ok->setPosition(ccp(160,150));
	mainLayer->addChild(btn_ok);

	btn_close = ImageManager::getSpriteWithXml2(kImageSheet_freemium,"button_close");
	btn_close->setPosition(ccp(290,420));
	mainLayer->addChild(btn_close);

	isClickedEnable = false;

	return true;
}

void Popup_giftParcel_3::openPopup()
{
	mainLayer->setPosition(ccp(-winSize.width, 0));
	CCMoveTo *move = CCMoveTo::create(0.2, ccp(0,0));
	CCEaseExponentialOut* accel = CCEaseExponentialOut::create(move);
	mainLayer->runAction(accel);

	CCFadeTo* fade = CCFadeTo::create(0.2, 200);
	CCDelayTime *cdt = CCDelayTime::create(0.3);
	CCCallFunc *cf = CCCallFunc::create(this,callfunc_selector(Popup_giftParcel_3::clickedEnable));
	this->runAction(CCSequence::create(fade,cdt,cf,NULL));
	SoundManager::PlaySoundEffect(sndEffect_zoomin);
}


void Popup_giftParcel_3::closePopup()
{
	CCMoveTo *move = CCMoveTo::create(0.2, ccp(-winSize.width,0));
	CCEaseExponentialIn* accel = CCEaseExponentialIn::create(move);
	mainLayer->runAction(accel);

	CCFadeTo* fade = CCFadeTo::create(0.2, 0);
	CCCallFunc *cf = CCCallFunc::create(this,callfunc_selector(Popup_giftParcel_3::removeFromParent));
	this->runAction(CCSequence::create(fade,cf,NULL));
	SoundManager::PlaySoundEffect(sndEffect_zoomout);
}


int Popup_giftParcel_3::touchEnded(CCPoint pt)
{
	if (isClickedEnable)
	{
		if (btn_close->boundingBox().containsPoint(pt))
		{
			return giftParcel_3_btn_close;
		}

		else if (btn_ok->boundingBox().containsPoint(pt))
		{
			return giftParcel_3_btn_ok;
		}
	}

	return -1;
}

void Popup_giftParcel_3::clickedEnable()
{
	this->isClickedEnable = true;
}